<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="文档管理">
<meta name="author" content="文档管理">
<title>Web 框架集成 文档管理</title>
<link href="./assets/css/bootstrap.min.css" rel="stylesheet">
<!-- Documentation extras -->
<link href="./assets/css/docs.css" rel="stylesheet">
<link href="./assets/css/style.css" rel="stylesheet">
<link href="http://cdn.bootcss.com/highlight.js/7.3/styles/github.min.css" rel="stylesheet">
<style>
body{font-family:"ff-tisa-web-pro-1","ff-tisa-web-pro-2","Lucida Grande","Helvetica Neue",Helvetica,Arial,"Hiragino Sans GB","Hiragino Sans GB W3","WenQuanYi Micro Hei",sans-serif;}
h1, .h1, h2, .h2, h3, .h3, h4, .h4, .lead {font-family:"ff-tisa-web-pro-1","ff-tisa-web-pro-2","Lucida Grande","Helvetica Neue",Helvetica,Arial,"Hiragino Sans GB","Hiragino Sans GB W3","Microsoft YaHei UI","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
pre code { background: transparent; }
@media (min-width: 768px) {
    .bs-docs-home .bs-social, 
    .bs-docs-home .bs-masthead-links {
      margin-left: 0;
    }
}
.bs-docs-section p {
	line-height: 2;
}
.bs-docs-section p.lead {
	line-height: 1.4;
}
</style>
<!--[if lt IE 9]><script src="../docs-assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
  <script src="http://cdn.bootcss.com/html5shiv/3.7.0/html5shiv.min.js"></script>
  <script src="http://cdn.bootcss.com/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<!-- Favicons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="../docs-assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="shortcut icon" href="../docs-assets/ico/favicon.png">
  </head>
  <body>
    <a class="sr-only" href="#content">Skip to main content</a>

    <!-- Docs master nav -->
    <header class="navbar navbar-inverse navbar-fixed-top bs-docs-nav" role="banner">
  <div class="container">
    <div class="navbar-header">
      <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="./overview.html" class="navbar-brand">文档管理</a>
    </div>
    <nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
      <ul class="nav navbar-nav">
       		<li  ><a href="overview.html">简介</a></li>
            <li ><a href="download.html">下载</a></li>
            <li ><a href="userguide.html">开发指南</a></li>
            <li ><a href="config.html">配置指南</a></li>
            <li ><a href="syntax.html">语法指南</a></li>
            <li class='active'><a href="integrate.html" class="selected">Web 框架集成</a></li>
            <!--
            <li ><a href="example.html">例子</a></li>
            -->
            <li ><a href="faq.html">FAQ</a></li>
            <li ><a href="about.html">关于</a></li>
      </ul>
    </nav>
  </div>
</header>
    <!-- Docs page layout -->
    <div class="bs-header" id="content">
      <div class="container">
        <h2>Web 框架集成</h2>
        <p></p>
      </div>
    </div>
	<a href="#top" id="toTop"></a>
    <!-- Callout for the old docs link -->
    <div class="container bs-docs-container">
      <div class="row">
        <div class="col-md-3">
          <div class="bs-sidebar hidden-print" role="complementary">
            <ul class="nav bs-sidenav">
            <li><a href="#x1016944">1 简述</a></li>
            <li><a href="#x1336408494">2 Web 中的默认隐含对象</a></li>
            <li><a href="#x1694283446">3 JetEngine 自动加载方式</a></li>
            <li><a href="#x1896411413">4 各种集成方式介绍</a></li>
            <li><a href="#x1487897129">&nbsp&nbsp4.1 直接使用 HttpServlet</a></li>
            <li><a href="#x554032752">&nbsp&nbsp4.2 直接使用 Filter 模式</a></li>
            <li><a href="#x1633219041">&nbsp&nbsp4.3 Struts 2.x</a></li>
            <li><a href="#x941308089">&nbsp&nbsp4.4 Spring MVC</a></li>
            <li><a href="#x2108526772">&nbsp&nbsp4.5 JFinal</a></li>
            <li><a href="#x2439853">&nbsp&nbsp4.6 Nutz</a></li>
            <li><a href="#x2314405">&nbsp&nbsp4.7 Jodd</a></li>
            </ul>
          </div>
        </div>
	        <div class="col-md-9" role="main">
	         <div class="bs-docs-section">
	           <h1 id="x1016944" class="page-header"><span>1 简述</span></h1>
<p>jetbrick-template 除了作为普通的模板引擎嵌入在 Application 中外，大部分情况下还会和各种 WebMVC 框架整合作为前端的 View，来代替过时的 JSP 或者 Velocity。</p>
<p>目前已近集成了几种流行的 Web 框架：</p> 
<ul> 
 <li><a href="#HttpServlet">HttpServlet</a></li> 
 <li><a href="#Filter">Filter</a></li> 
 <li><a href="#Struts">Struts 2.x</a></li> 
 <li><a href="#SpringMVC">Spring MVC</a></li> 
 <li><a href="#JFinal">JFinal</a></li> 
 <li><a href="#Nutz">Nutz</a></li> 
 <li><a href="#Jodd">Jodd</a></li> 
</ul>
<p><a href="download.html#samples">点击这里下载各种集成方式的演示 demo</a></p>
<h1 id="x1336408494" class="page-header"><span>2 Web 中的默认隐含对象</span></h1>
<p>当 jetbrick-template 被用作 Web 应用中时候，会自动引入下面的对象，这些对象在所有的模板中全局可访问。</p> 
<table> 
 <thead> 
  <tr> 
   <th>隐含对象 </th> 
   <th>类 型 </th> 
   <th>说 明 </th> 
  </tr> 
 </thead> 
 <tbody> 
  <tr> 
   <td>context </td> 
   <td>JetConext </td> 
   <td> </td> 
  </tr> 
  <tr> 
   <td>servletContext </td> 
   <td>ServletContext </td> 
   <td> </td> 
  </tr> 
  <tr> 
   <td>session </td> 
   <td>HttpSession </td> 
   <td> </td> 
  </tr> 
  <tr> 
   <td>request </td> 
   <td>HttpServletRequest </td> 
   <td> </td> 
  </tr> 
  <tr> 
   <td>response </td> 
   <td>HttpServletResponse </td> 
   <td> </td> 
  </tr> 
  <tr> 
   <td>applicationScope </td> 
   <td>Map&lt;String,Object&gt; </td> 
   <td>快捷访问 servletContext.getAttribute(name) </td> 
  </tr> 
  <tr> 
   <td>sessionScope </td> 
   <td>Map&lt;String,Object&gt; </td> 
   <td>快捷访问 session.getAttribute(name) </td> 
  </tr> 
  <tr> 
   <td>requestScope </td> 
   <td>Map&lt;String,Object&gt; </td> 
   <td>快捷访问 request.getAttribute(name) </td> 
  </tr> 
  <tr> 
   <td>parameter </td> 
   <td>Map&lt;String,String&gt; </td> 
   <td>快捷访问 request.getParameter(name) </td> 
  </tr> 
  <tr> 
   <td>parameterValues </td> 
   <td>Map&lt;String,String[]&gt; </td> 
   <td>快捷访问 request.getParameterValues(name) </td> 
  </tr> 
  <tr> 
   <td>ctxpath </td> 
   <td>String </td> 
   <td>快捷访问 request.getContextPath() </td> 
  </tr> 
  <tr> 
   <td>webroot </td> 
   <td>String </td> 
   <td>返回完整的webapp路径: <a href="http://127.0.0.1:8080/myapp">http://127.0.0.1:8080/myapp</a> </td> 
  </tr> 
 </tbody> 
</table>
<p><strong>下面的例子演示了如何使用这些隐含变量：</strong></p>
<p>模板如下：</p> 
<div class="highlight">
 <pre><code class="language-html">request.requestURI == ${request.requestURI}
request.getParameter(&quot;name&quot;) == ${parameter.name}
request.getAttribute(&quot;items&quot;) == ${requestScope.items}
session.getAttribute(&quot;user&quot;) == ${sessionScope.user}
</code></pre>
</div>
<p>特别需要说明的一点是：模板中使用或者声明的全局变量不光会从 <code>JetConext context</code> 中获取，在 Web 应用中，还会从 <code>requestScope</code>，<code>sessionScope</code>，<code>applicationScope</code> 中查找对应的内容。</p>
<p>默认的查顺序如下：</p> 
<ol> 
 <li>context</li> 
 <li>requestScope</li> 
 <li>sessionScope</li> 
 <li>applicationScope</li> 
</ol>
<p>也就是说，如果存在 <code>request.getAttribute(&quot;user&quot;)</code> 的情况下 <code>${user}</code> 等价于 <code>${requestScope.user}</code>。</p>
<p><a name="JetEngine"></a></p>
<h1 id="x1694283446" class="page-header"><span>3 JetEngine 自动加载方式</span></h1>
<p>需要在 web.xml 中进行配置，下面两个配置项都是可选项。</p> 
<div class="highlight">
 <pre><code class="language-html">&lt;context-param&gt;
  &lt;param-name&gt;jetbrick-template-config-location&lt;/param-name&gt;
  &lt;param-value&gt;/WEB-INF/jetbrick-template.properties&lt;/param-value&gt;
&lt;/context-param&gt;

&lt;listener&gt;
  &lt;listener-class&gt;jetbrick.template.web.JetWebEngineLoader&lt;/listener-class&gt;
&lt;/listener&gt;
</code></pre>
</div>
<p>如果没有配置 <code>jetbrick-template-config-location</code> 参数，那么配置文件默认从 classpath 根目录下面查找 jetbrick-template.properties。</p>
<p>如果没有配置 JetWebEngineLoader 启动监听器，那么 JetEngine 也会在模板第一次请求的时候自动初始化。配置成 Listener，可以在 Webapp 启动的时候马上进行初始化。</p>
<p><strong>注意：</strong></p> 
<ol> 
 <li><p>在 Web 集成模式中，采用以下的默认值：</p> 
  <div class="highlight">
   <pre><code class="language-html">template.loader = jetbrick.template.web.WebResourceLoader
template.path = /
</code></pre>
  </div></li> 
 <li><p>对于 <code>WebResourceLoader</code> 的来说，<code>template.path</code> 的路径相对于 webapp 的根目录。</p> 
  <div class="highlight">
   <pre><code class="language-html">/ == servletContext.getRealPath(&quot;/&quot;)
/WEB-INF/jetx == servletContext.getRealPath(&quot;/WEB-INF/jetx&quot;)
</code></pre>
  </div></li> 
 <li><p>jetbrick-template 内置和其他 Web 框架的集成方式都可以用这两个配置想进行全局初始化。</p></li> 
 <li><p>在 web 集成模式中，<code>JetEngine</code> 是单例的，可以通过 <code>JetWebEngineLoader.getJetEngine()</code> 获取。</p></li> 
</ol>
<h1 id="x1896411413" class="page-header"><span>4 各种集成方式介绍</span></h1>
<p><a name="HttpServlet"></a></p>
<h2 id="x1487897129" class="page-header"><span>4.1 直接使用 HttpServlet</span></h2>
<p>jetbrick-template 可以直接作为 HttpServlet 使用。需要在 web.xml 中作如下配置。</p> 
<div class="highlight">
 <pre><code class="language-html">&lt;servlet&gt;
  &lt;servlet-name&gt;jetbrick-template&lt;/servlet-name&gt;
  &lt;servlet-class&gt;jetbrick.template.web.servlet.JetTemplateServlet&lt;/servlet-class&gt;
  &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
  &lt;servlet-name&gt;jetbrick-template&lt;/servlet-name&gt;
  &lt;url-pattern&gt;*.jetx&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</code></pre>
</div>
<p>最简单，也是最直接的方式。打开浏览器访问 <a href="http://127.0.0.1:8080/index.jetx">http://127.0.0.1:8080/index.jetx</a> 看看效果吧。</p>
<p>具体例子代码参考： <a href="https://github.com/subchen/jetbrick-template-webmvc-samples/">https://github.com/subchen/jetbrick-template-webmvc-samples/</a></p>
<p><a name="Filter"></a></p>
<h2 id="x554032752" class="page-header"><span>4.2 直接使用 Filter 模式</span></h2>
<p>jetbrick-template 可以直接作为 Filter 使用。需要在 web.xml 中作如下配置。</p> 
<div class="highlight">
 <pre><code class="language-html">&lt;filter&gt;
  &lt;filter-name&gt;jetbrick-template&lt;/filter-name&gt;
  &lt;filter-class&gt;jetbrick.template.web.servlet.JetTemplateFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
  &lt;filter-name&gt;jetbrick-template&lt;/filter-name&gt;
  &lt;url-pattern&gt;*.jetx&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</code></pre>
</div>
<p><a name="Struts"></a></p>
<h2 id="x1633219041" class="page-header"><span>4.3 Struts 2.x</span></h2>
<p>jetbrick-template 可以和 Struts 2.x 进行集成。</p>
<p>首先需要对 Struts 进行如下配置（<code>struts.xml</code>），这个配置是告诉 Struts 使用 <code>jetbrick.template.web.struts.JetTemplateResult</code> 这个类来处理采用 jetbrick-template 格式的模板：</p> 
<div class="highlight">
 <pre><code class="language-html">&lt;result-types&gt;
  &lt;result-type name=&quot;jetx&quot; class=&quot;jetbrick.template.web.struts.JetTemplateResult&quot; /&gt;
&lt;/result-types&gt;
</code></pre>
</div>
<p>接下来配置你的 action 如下：</p> 
<div class="highlight">
 <pre><code class="language-html">&lt;action name=&quot;index&quot; class=&quot;jetbrick.template.samples.struts.action.IndexAction&quot;&gt;
  &lt;result type=&quot;jetx&quot;&gt;/index.jetx&lt;/result&gt;
&lt;/action&gt;
</code></pre>
</div>
<p>打开浏览器访问 <a href="http://127.0.0.1:8080/index.do">http://127.0.0.1:8080/index.do</a> 看看效果吧。</p>
<p>具体例子代码参考： <a href="https://github.com/subchen/jetbrick-template-webmvc-samples/">https://github.com/subchen/jetbrick-template-webmvc-samples/</a></p>
<p><a name="SpringMVC"></a></p>
<h2 id="x941308089" class="page-header"><span>4.4 Spring MVC</span></h2>
<p>jetbrick-template 可以和 Spring MVC 进行集成。</p>
<p>配置方式如下：</p> 
<div class="highlight">
 <pre><code class="language-html">&lt;bean id=&quot;viewResolver&quot; class=&quot;jetbrick.template.web.springmvc.JetTemplateViewResolver&quot;&gt;
  &lt;property name=&quot;suffix&quot; value=&quot;.jetx&quot; /&gt;
  &lt;property name=&quot;contentType&quot; value=&quot;text/html; charset=utf-8&quot; /&gt;
  &lt;property name=&quot;order&quot; value=&quot;9&quot; /&gt;
&lt;/bean&gt;
</code></pre>
</div>
<p>具体例子代码参考： <a href="https://github.com/subchen/jetbrick-template-webmvc-samples/">https://github.com/subchen/jetbrick-template-webmvc-samples/</a></p>
<p><a name="JFinal"></a></p>
<h2 id="x2108526772" class="page-header"><span>4.5 JFinal</span></h2>
<p>jetbrick-template 可以和 JFinal 进行集成。</p> 
<ol> 
 <li><p>修改 JFinal 主配置文件</p> 
  <div class="highlight">
   <pre><code class="language-html">public class JetxConfig extends JFinalConfig {
    @Override
    public void configConstant(Constants me) {
        me.setMainRenderFactory(new JetTemplateRenderFactory());
        ...
    }
    ...
}
</code></pre>
  </div></li> 
 <li><p>新建一个 Controller</p> 
  <div class="highlight">
   <pre><code class="language-html">public class UsersController extends Controller {
    public void index() {
        setAttr(&quot;userlist&quot;, DaoUtils.getUserList());
        render(&quot;/users.jetx&quot;);
    }
}
</code></pre>
  </div></li> 
</ol>
<p>可以了，就这么简单!</p>
<p>具体例子代码参考： <a href="https://github.com/subchen/jetbrick-template-webmvc-samples/">https://github.com/subchen/jetbrick-template-webmvc-samples/</a></p>
<p><a name="Nutz"></a></p>
<h2 id="x2439853" class="page-header"><span>4.6 Nutz</span></h2>
<p>感谢 wendal (<a href="mailto:wendal1985@gmail.com">wendal1985@gmail.com</a>) 提供相关代码。</p>
<p>View： <code>jetbrick.template.web.nutz.JetTemplateView</code><br />ViewMaker：<code>jetbrick.template.web.nutz.JetTemplateViewMaker</code></p> 
<ol> 
 <li><p>将视图工厂整合进应用中：</p><p>在主模块上，加：<code>@Views({JetTemplateViewMaker.class})</code> 注解</p> 
  <div class="highlight">
   <pre><code class="language-html">@Views({JetTemplateViewMaker.class})
@...
public class MainModule {
}
</code></pre>
  </div></li> 
 <li><p>使用 jetx 视图：</p> 
  <div class="highlight">
   <pre><code class="language-html">@At
@Ok(&quot;.jetx:/WEB-INF/html/user_info.jetx&quot;)
public String list(@Param(&quot;name&quot;) String name, HttpServletRequest request){
  if (name != null) return name;
  return &quot;测试&quot;;
}
</code></pre>
  </div></li> 
 <li><p>模板中使用:</p> 
  <div class="highlight">
   <pre><code class="language-html">#define(String obj)
${obj}
</code></pre>
  </div></li> 
 <li><p>获得输出：</p> 
  <div class="highlight">
   <pre><code class="language-html">测试
</code></pre>
  </div></li> 
</ol>
<p>具体例子代码参考： <a href="https://github.com/subchen/jetbrick-template-webmvc-samples/">https://github.com/subchen/jetbrick-template-webmvc-samples/</a></p>
<p><a name="Jodd"></a></p>
<h2 id="x2314405" class="page-header"><span>4.7 Jodd</span></h2> 
<ol> 
 <li><p>首先需要配置 Jodd 的配置文件：madvoc.props</p> 
  <div class="highlight">
   <pre><code class="language-html">[jetbrick.template.web.jodd.JetTemplateResult]
contentType=text/html; charset=UTF-8

[automagicMadvocConfigurator]
includedEntries=jodd.*,jetbrick.template.web.jodd.*,yourapp.jodd.action.*
</code></pre>
  </div></li> 
 <li><p>Action 例子</p> 
  <div class="highlight">
   <pre><code class="language-html">@MadvocAction
public class UsersAction {
    @Out
    Collection&lt;UserInfo&gt; userlist;

    @Action(extension = Action.NONE)
    public Object view() {
        userlist = DaoUtils.getUserList();
        return &quot;jetx:/users.jetx&quot;;
    }
}
</code></pre>
  </div></li> 
 <li><p>jetx 例子</p> 
  <div class="highlight">
   <pre><code class="language-html">&lt;table border=&quot;1&quot; width=&quot;600&quot;&gt;
  &lt;tr&gt;
    &lt;td&gt;ID&lt;/td&gt;
    &lt;td&gt;姓名&lt;/td&gt;
    &lt;td&gt;邮箱&lt;/td&gt;
    &lt;td&gt;书籍&lt;/td&gt;
  &lt;/tr&gt;
  #for(UserInfo user: userlist)
  &lt;tr&gt;
    &lt;td&gt;${user.id}&lt;/td&gt;
    &lt;td&gt;${user.name}&lt;/td&gt;
    &lt;td&gt;${user.email}&lt;/td&gt;
    &lt;td&gt;&lt;a href=&quot;books?author=${user.id}&quot;&gt;书籍列表&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
  #end
&lt;/table&gt;
</code></pre>
  </div></li> 
</ol>
<p>具体例子代码参考： <a href="https://github.com/subchen/jetbrick-template-webmvc-samples/">https://github.com/subchen/jetbrick-template-webmvc-samples/</a></p>
           	</div>
           </div>
		</div> 
 	</div>
    <!-- Footer
    ================================================== -->
    <footer class="bs-footer" role="contentinfo">
      <div class="container">
        <ul class="footer-links">
          <li>当前版本： v0.0.3</li>
          <li class="muted">&middot;</li>
          <li><a href="http://v2.bootcss.com/">Bootstrap 2.3.2 中文文档</a></li>
          <li class="muted">&middot;</li>
          <li><a href="http://www.bootcss.com">Bootstrap中文网</a></li>
          <li class="muted">&middot;</li>
          <li><a href="http://blog.getbootstrap.com">官方博客</a></li>
          <li class="muted">&middot;</li>
          <li><a href="https://github.com/twbs/bootstrap/issues?state=open">Issues</a></li>
          <li class="muted">&middot;</li>
          <li><a href="https://github.com/twbs/bootstrap/releases">Releases</a></li>
        </ul>
      </div>
    </footer>

    <!-- JS and analytics only. -->
<script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<script src="./assets/js/bootstrap.js"></script>
<script src="http://cdn.bootcss.com/holder/2.0/holder.min.js"></script>
<script src="http://cdn.bootcss.com/highlight.js/7.3/highlight.min.js"></script>
<script >hljs.initHighlightingOnLoad();</script>
<script src="./assets/js/application.js"></script>
<script src="./assets/js/jquery.scrollToTop.min.js"></script>
<script text="javascript/text">
	$(document).ready(function(){
		$("#toTop").scrollToTop(1000);
  		$("table").addClass("table table-bordered");
  	});
</script>
  </body>
</html>
